import util from './util.js'
// #ifdef MP-WEIXIN || MP-ALIPAY
const config = {
	baseUrl: 'http://localhost:9999/admin',
	// baseUrl: 'https://www.allinsport.top/api/admin',
	// baseUrl: 'http://6b78c246.r24.cpolar.top/admin'
}
// #endif

// #ifdef H5
/* 动态引入script，import动态导入修改不生效1*/
const script = document.createElement('script')
script.src = 'static/config/config.js'
document.head.appendChild(script)
if (script.addEventListener) {
	script.addEventListener('load', function() {
		console.log('config配置文件', config)
		if (!config) {
			return
		}
		getApp().globalData.mqttIp = config.mqttIp
		getApp().globalData.configInfo = config
	}, false);
} else if (script.attachEvent) {
	script.attachEvent('onreadystatechange', function() {
		var target = window.event.srcElement;
		if (target.readyState == 'loaded') {
			console.log('config配置文件1', config)
			if (!config) {
				return
			}
			getApp().globalData.mqttIp = config.mqttIp
			getApp().globalData.configInfo = config
		}
	});
}
// #endif

const api = {
	login: '/api/v1/app/access',
	// getBaseUserInfo1: '/api/v1/app/gateway/getBaseUserInfo', // 获取用户信息
	// getImg: 'getImg', // 首页-获取图片
	// getSaleInfo: 'getSaleInfo', // 首页-获取商品

	// calendar: '/api/v1/app/gateway/getSaleInfo', // 送奶日历-日历
	// dayDetail: '/api/v1/app/gateway/dayDetail', // 送奶日历-某天送奶详情

	// getProductList: '/api/v1/app/gateway/getProductList', // 我的-获取产品列表
	// designate: '/api/v1/app/gateway/designate', // 我的-立即配送

	// userBind: '/api/v1/app/gateway/userBind', // 我的-设置-保存用户信息
	// opForceBatch: '/api/v1/app/gateway/opForceBatch', //水控-批量或者单个开关阀
}

export function serviceApi(param, url, method, id) {
	const baseUrl = config.baseUrl
	if (!url) {
		uni.showToast({
			icon: 'none',
			title: '出错啦~'
		});
		return;
	}
	const reqUrl = url === 'login' ? `${baseUrl}${api[url]}` : `${baseUrl}/api/v1/app/gateway`
	// const reqUrl = `${baseUrl}/api/v1/app/gateway`
	let header = {}
	const p = {
		method: url
	}
	if (url != 'login') {
		const token = util.getToken()
		if (token) {
			header['Authorization'] = token
		}
		p.param = param
	} else {
		p.code = param.code
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: reqUrl,
			header: header,
			method: method ? method : 'POST',
			timeout: 30 * 1000,
			data: p,
			success(res) {
				if (res.statusCode == 200 && res.data) {
					console.info(url + "-res:", res.data);
					if (res.data.code == 0) {
						resolve(res.data)
					} else {
						if (res.data.code == 401) {
							const casFlag = uni.getStorageInfoSync('cloud-water-user-cas')
							if (casFlag) {
								// uni.showModal({
								// 	title: '提示',
								// 	content: '哈哈哈',
								// 	showCancel: false,
								// })
								uni.reLaunch({
									url: '/pages_other/error/error?code=401'
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: 'token过期,请重新进入应用'
								});
								uni.clearStorage()
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/index/index'
									})
								}, 1500)
							}
							return
						} else if (res.data.code == 403) {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						} else if (res.data.code == 500) {
							uni.showToast({
								icon: 'none',
								title: '接口错误'
							});
						}
						reject({
							code: res.data.code,
							message: res.data.msg
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '连接失败（' + res.statusCode + '）'
					})
				}
			},
			fail(res) {
				// console.log("serviceApi fail:url=" + methodUrl + "res=" + JSON.stringify(res))
				resolve({
					result: -1,
					message: '请求出错'
				});
			}
		})
	})
}